home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection Student Program / ADC Tools Sampler CD Disk 3 1999.iso / Metrowerks CodeWarrior / Java Support / Java_Source / IFC_112 / netscape / application / DragSession.java < prev    next >
Encoding:
Text File  |  1999-05-28  |  12.5 KB  |  377 lines  |  [TEXT/CWIE]

  1. // DragSession.java
  2. // By Ned Etcode
  3. // Copyright 1996, 1997 Netscape Communications Corp.  All rights reserved.
  4.  
  5. package netscape.application;
  6.  
  7. import netscape.util.*;
  8.  
  9. /** Object subclass that implements "drag-and-drop" within the IFC. A
  10.   * DragSession allows an Image to represent some data, and the user can
  11.   * drag that Image from one View to another. For example, in the case of
  12.   * dragging Colors, a red square Image would visually represent the red
  13.   * Color instance, declared as the DragSession's <B>data</b>.<br><br>A
  14.   * drag-and-drop session begins with a mouse down event in a View. That View's
  15.   * <b>mouseDown()</b>
  16.   * creates a DragSession, and from then on the DragSession's Image moves as
  17.   * the user moves the mouse.  All Views it passes over receive
  18.   * <B>acceptsDrag()</B> messages allowing them to return an object to act as
  19.   * the DragSession's DragDestination. A DragSession receives
  20.   * <b>dragEntered()</b>, <b>dragMoved()</b> and <b>dragExited()</b> messages
  21.   * as the Image traverses the View. On release, the current DragDestination
  22.   * receives a <B>dragDropped()</b> message, in which it should accept the
  23.   * DragSession's data by returning <b>true</b>, or reject it by returning
  24.   * <b>false</b>. If rejected, the DragSession Image optionally animates back
  25.   * to its origin. Ultimately, the DragSource receives a notification
  26.   * that the drag session has completed.
  27.   * @note 1.0 added private method to support creating the drag view
  28.   * @note 1.0 mouseDrag/mouseup went public with a private tag
  29.   * @note 1.0 some point conversion fixes
  30.   * @note 1.0 added accessor to isAccepting. added private tag
  31.   * @note 1.1 Fixed destination()
  32.   */
  33. public class DragSession {
  34.     String              dataType;
  35.     Object              data;
  36.     Image               image;
  37.     int                 initialX, initialY;
  38.     int                 mouseDownX, mouseDownY;
  39.     int                 mouseX, mouseY;
  40.     DragSource          source;
  41.     DragDestination     destination;
  42.     DragView            dragView;
  43.     int                 modifiers;
  44.     RootView            rootView;
  45.     View                sourceView;
  46.     View                destinationView;
  47.     boolean             isAccepting;
  48.  
  49.     /** Drag session modifier flag. */
  50.     public static final int     SHIFT_MASK = KeyEvent.SHIFT_MASK;
  51.     /** Drag session modifier flag. */
  52.     public static final int     CONTROL_MASK = KeyEvent.CONTROL_MASK;
  53.     /** Drag session modifier flag. */
  54.     public static final int     META_MASK = KeyEvent.META_MASK;
  55.     /** Drag session modifier flag. */
  56.     public static final int     ALT_MASK = KeyEvent.ALT_MASK;
  57.  
  58.     /** @private */
  59.     public DragSession(DragSource source, Image image,
  60.                        int initialX, int initialY,
  61.                        int mouseDownX, int mouseDownY,
  62.                        String dataType, Object data,
  63.                        boolean createDragView) {
  64.         Point initialPoint = new Point(), mousePoint = new Point();
  65.  
  66.         sourceView = source.sourceView(this);
  67.         rootView = sourceView.rootView();
  68.  
  69.         sourceView.convertToView(null, initialX, initialY, initialPoint);
  70.         sourceView.convertToView(null, mouseDownX, mouseDownY, mousePoint);
  71.  
  72.         /// ALERT - Check this out
  73.         if (rootView.windowClipView() != null) {
  74.             rootView.convertPointToView(rootView.windowClipView(),
  75.                                          initialPoint, initialPoint);
  76.             rootView.convertPointToView(rootView.windowClipView(),
  77.                                          mousePoint, mousePoint);
  78.         }
  79.  
  80.         this.source = source;
  81.         this.image = image;
  82.         this.initialX = initialPoint.x;
  83.         this.initialY = initialPoint.y;
  84.         this.mouseDownX = mousePoint.x;
  85.         this.mouseDownY = mousePoint.y;
  86.         this.dataType = dataType;
  87.         this.data = data;
  88.  
  89.         if (createDragView) {
  90.             dragView = new DragView(this);
  91.         }
  92.     }
  93.  
  94.     /** Constructs a DragSession, represented visually by <b>image</b>.
  95.       * <b>initialX</b> and <b>initialY</b> specify the Image's initial
  96.       * location, and <b>mouseDownX</b> and <b>mouseDownY</b> specify the
  97.       * mouse's initial location. Both points should be expressed in
  98.       * terms of the coordinate system of the View returned by <b>source</b>.
  99.       * This source View also determines the RootView in which the
  100.       * DragSession's Image appears.  <b>dataType</b> allows for additional
  101.       * information about the drag data <b>data</b>.
  102.       */
  103.     public DragSession(DragSource source, Image image,
  104.                        int initialX, int initialY,
  105.                        int mouseDownX, int mouseDownY,
  106.                        String dataType, Object data) {
  107.         this(source, image, initialX, initialY, mouseDownX, mouseDownY,
  108.              dataType, data, true);
  109.     }
  110.  
  111.     /** Returns the DragSession's <b>data</b>.
  112.       * @see #setData
  113.       */
  114.     public Object data() {
  115.         return data;
  116.     }
  117.  
  118.     /** Sets the DragSession's <b>data</b>, the data being dragged in
  119.       * the drag session.
  120.       */
  121.     public void setData(Object data) {
  122.         this.data = data;
  123.     }
  124.  
  125.     /** Returns the DragSession's data's type.
  126.       * @see #setDataType
  127.       */
  128.     public String dataType() {
  129.         return dataType;
  130.     }
  131.  
  132.     /** Sets the DragSession's data type information to <b>dataType</b>.  The
  133.       * data type allows for additional information about the drag data.
  134.       */
  135.     public void setDataType(String dataType) {
  136.         this.dataType = dataType;
  137.     }
  138.  
  139.     /** Returns the DragSession's "source."  A session's drag source cannot
  140.       * change once a session has begun.
  141.       */
  142.     public DragSource source() {
  143.         return source;
  144.     }
  145.  
  146.     /** Returns the DragSession's current DragDestination. The DragSession
  147.       * determines the current destination by calling the <b>acceptsDrag()</b>
  148.       * method of the View under the mouse.
  149.       * @see View#acceptsDrag
  150.       */
  151.     public DragDestination destination() {
  152.         return destination;
  153.     }
  154.  
  155.     /** Returns an integer representing the modifier keys (Shift, Control,
  156.       * Meta, Alternate) held down during the most recent mouse event.
  157.       * Bitwise AND this value with one of the class' modifier flags, or call
  158.       * one of the explicit methods such as <b>isShiftKeyDown()</b>.
  159.       * @see #isShiftKeyDown
  160.       * @see #isControlKeyDown
  161.       * @see #isMetaKeyDown
  162.       * @see #isAltKeyDown
  163.       */
  164.     public int dragModifiers() {
  165.         return modifiers;
  166.     }
  167.  
  168.     /** Returns <b>true</b> if the Shift Key was held down during the most
  169.       * recent mouse event.
  170.       */
  171.     public boolean isShiftKeyDown() {
  172.         return (modifiers & SHIFT_MASK) != 0;
  173.     }
  174.  
  175.     /** Returns <b>true</b> if the Control Key was held down during the most
  176.       * recent mouse event.
  177.       */
  178.     public boolean isControlKeyDown() {
  179.         return (modifiers & CONTROL_MASK) != 0;
  180.     }
  181.  
  182.     /** Returns <b>true</b> if the Meta Key was held down during the most
  183.       * recent mouse event.
  184.       */
  185.     public boolean isMetaKeyDown() {
  186.         return (modifiers & META_MASK) != 0;
  187.     }
  188.  
  189.     /** Returns <b>true</b> if the Alternate Key was held down during the most
  190.       * recent mouse event.
  191.       */
  192.     public boolean isAltKeyDown() {
  193.         return (modifiers & ALT_MASK) != 0;
  194.     }
  195.  
  196.     void updateModifiers(MouseEvent currentEvent) {
  197.         modifiers = 0;
  198.         if (currentEvent == null) {
  199.             return;
  200.         }
  201.  
  202. //        x = currentEvent.x + bounds.x;
  203. //        y = currentEvent.y + bounds.y;
  204.  
  205.         if (currentEvent.isShiftKeyDown()) {
  206.             modifiers += SHIFT_MASK;
  207.         }
  208.         if (currentEvent.isControlKeyDown()) {
  209.             modifiers += CONTROL_MASK;
  210.         }
  211.         if (currentEvent.isMetaKeyDown()) {
  212.             modifiers += META_MASK;
  213.         }
  214.         if (currentEvent.isAltKeyDown()) {
  215.             modifiers += ALT_MASK;
  216.         }
  217.     }
  218.  
  219.     /** @private */
  220.     public void mouseDragged(MouseEvent event) {
  221.         int x, y;
  222.         DragDestination dest = null;
  223.         View view;
  224.         Point aPoint = new Point(event.x, event.y);
  225.  
  226.         if (dragView != null) {
  227.             dragView.convertPointToView(null, aPoint, aPoint);
  228.         }
  229.         x = aPoint.x;
  230.         y = aPoint.y;
  231.  
  232.         updateModifiers(event);
  233.         view = rootView.viewForMouse(x, y);
  234.  
  235.         /* Cannot dragEntered() a view that is not in the
  236.          * current modal session if any.
  237.          */
  238.         if( rootView.viewExcludedFromModalSession(view))
  239.             view = null;
  240.  
  241.         if (view != null) {
  242.             Point point = Point.newPoint();
  243.  
  244.             rootView.convertToView(view, x, y, point);
  245.             dest = view.acceptsDrag(this, point.x, point.y);
  246.  
  247.             // Go up the view hierarchy looking for the first view
  248.             // that can accept the drag.
  249.             while (dest == null && view._superview != null) {
  250.                 point.x += view.bounds.x;
  251.                 point.y += view.bounds.y;
  252.                 view = view._superview;
  253.                 dest = view.acceptsDrag(this, point.x, point.y);
  254.             }
  255.             Point.returnPoint(point);
  256.         }
  257.  
  258.         if (destination == null && dest != null) {
  259.             destination = dest;
  260.             destinationView = view;
  261.             isAccepting = destination.dragEntered(this);
  262.         } else if (destination != null && dest == null) {
  263.             destination.dragExited(this);
  264.             destination = null;
  265.             destinationView = null;
  266.         } else if (destination != dest) {
  267.             destination.dragExited(this);
  268.             destination = dest;
  269.             destinationView = view;
  270.             destination.dragEntered(this);
  271.         } else if (destination != null) {
  272.             isAccepting = destination.dragMoved(this);
  273.         }
  274.     }
  275.  
  276.     /** @private */
  277.     public void mouseUp(MouseEvent event) {
  278.         boolean         accepted = false, animateBack = false;
  279.  
  280.         /* the try-finally will force the DragView to get removed from the
  281.          * screen even if user code throws an exception.  This is what you
  282.          * want
  283.          */
  284.         try {
  285.             updateModifiers(event);
  286.  
  287.             if (destination != null && isAccepting) {
  288.                 accepted = destination.dragDropped(this);
  289.             }
  290.  
  291.             if (accepted) {
  292.                 source.dragWasAccepted(this);
  293.             } else {
  294.                 animateBack = source.dragWasRejected(this);
  295.             }
  296.         } finally {
  297.             isAccepting = false;
  298.  
  299.             if (dragView != null) {
  300.                 if (animateBack) {
  301.                     dragView.startAnimatingRejectedDrag();
  302.                 } else {
  303.                     dragView.stopDragging();
  304.                 }
  305.             }
  306.         }
  307.     }
  308.  
  309.     /** Returns the View currently designated the DragSession's destination, or
  310.       * <b>null</b> if there is no such View.
  311.       */
  312.     public View destinationView() {
  313.         return destinationView;
  314.     }
  315.  
  316.     /** Returns the bounds of the dragged Image, in the current destination
  317.       * View's coordinate system.
  318.       */
  319.     public Rect destinationBounds() {
  320.         if (destinationView == null) {
  321.             return null;
  322.         } else {
  323.             Rect bounds = absoluteBounds();
  324.  
  325.             rootView.convertRectToView(destinationView, bounds, bounds);
  326.             return bounds;
  327.         }
  328.     }
  329.  
  330.     /** Returns the bounds of the dragged Image, in the DragSession's
  331.       * RootView's coordinate system.
  332.       */
  333.     public Rect absoluteBounds() {
  334.         Rect rect = new Rect(0, 0, image.width(), image.height());
  335.  
  336.         if (dragView != null) {
  337.             dragView.convertRectToView(null, rect, rect);
  338.         }
  339.         return rect;
  340.     }
  341.  
  342.     /** Returns the mouse's location, in the DragSession's RootView's
  343.       * coordinate system.
  344.       */
  345.     public Point absoluteMousePoint() {
  346.         if (dragView != null) {
  347.             Point point = new Point(dragView._lastX, dragView._lastY);
  348.  
  349.             dragView.superview().convertPointToView(null, point, point);
  350.             return point;
  351.         } else {
  352.             return new Point(0, 0);
  353.         }
  354.     }
  355.  
  356.     /** Returns the mouse's location, in the current destination View's
  357.       * coordinate system.
  358.       */
  359.     public Point destinationMousePoint() {
  360.         if (destinationView == null) {
  361.             return null;
  362.         } else if (dragView != null) {
  363.             Point point = new Point(dragView._lastX, dragView._lastY);
  364.  
  365.             dragView.superview().convertPointToView(destinationView, point, point);
  366.             return point;
  367.         } else {
  368.             return new Point(0, 0);
  369.         }
  370.     }
  371.  
  372.     /** @private */
  373.     public boolean destinationIsAccepting() {
  374.         return isAccepting;
  375.     }
  376. }
  377.